﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Remind.Model;
using Remind.BLL;
using Aptech.UI;
using System.Text.RegularExpressions;

namespace Remind.View
{
    public partial class frmContact : Form
    {
        int cid = 0;
        public static int faceid = 0;
        private SideBar _sidebar = null;
        private ImageList _imglist = null;
        private TelBLL tBLL = new TelBLL();
        private GroupBLL gBLL = new GroupBLL();
        private AddressBLL aBLL = new AddressBLL();
        private NetworkBLL nBLL = new NetworkBLL();
        private ContactsBLL cBLL = new ContactsBLL();

        public frmContact()
        {
            InitializeComponent();
        }

        public frmContact(SideBar sidebar, ImageList imglist)
        {
            InitializeComponent();
            _sidebar = sidebar;
            _imglist = imglist;
            this.cid = (int)sidebar.SeletedItem.Tag;
        }

        private void frmContact_Load(object sender, EventArgs e)
        {
            ContactsInfo contact = cBLL.GetContactByCid(cid);
            lblName.Text = contact.Cname;
            lblSex.Text = contact.Csex ? "男" : "女";
            cmbSex.SelectedText = contact.Csex ? "男" : "女";
            lblAge.Text = (DateTime.Now.Year - contact.Cbirthday.Year).ToString();
            lblBirthday.Text = contact.Cbirthday.Month + "月" + contact.Cbirthday.Day + "日";
            dpbirthday.Value = contact.Cbirthday;
            lblIdCard.Text = contact.IdCard;
            //绑定分组信息
            lblGroup.Text = gBLL.GetGroupInfoById(contact.Gid).Gname;
            faceid = contact.Cface >= _imglist.Images.Count ? 0 : contact.Cface;
            picboxinfo.Image = _imglist.Images[faceid];
            lblBz.Text = contact.CBz;
            //分别查询电话，地址，网络信息
            TelInfo tel = tBLL.GetTelByCid(cid);
            AddressInfo address = aBLL.GetAddressByCid(cid);
            NetworkInfo network = nBLL.GetNetWorkByCid(cid);
            //如果信息为空就插入一条信息
            if (tel != null)
                BindTel(tel);
            else
                InsertTel();
            if (address != null)
                BindAddress(address);
            else
                InsertAddress();
            if (network != null)
                BindNetwork(network);
            else
                InsertNetwork();
        }
        //插入电话信息
        private void InsertTel()
        {
            tBLL.InsertTel(new TelInfo { Cid = cid, Phone = "", Phone1 = "", Fax = "", Company = "", Family = "" });
        }
        //插入地址信息
        private void InsertAddress()
        {
            aBLL.InsertAddress(new AddressInfo { Cid = cid, Family = "", Company = "", Other = "" });
        }
        //插入网络信息
        private void InsertNetwork()
        {
            nBLL.InsertNetwork(new NetworkInfo { Cid = cid, Email = "", QQ = "", WebSite = "", Msn = "", Blog = "" });
        }
        //绑定电话信息
        private void BindTel(TelInfo tel)
        {
            lklblphone.Text = tel.Phone;
            lblphone1.Text = tel.Phone1;
            lblfamily.Text = tel.Family;
            lblcompany.Text = tel.Company;
            lblfax.Text = tel.Fax;
        }
        //绑定地址信息
        private void BindAddress(AddressInfo address)
        {
            lbladdcompany.Text = address.Company;
            lbladdfamily.Text = address.Family;
            lbladdother.Text = address.Other;
        }
        //绑定网络信息
        private void BindNetwork(NetworkInfo network)
        {
            lblemail.Text = network.Email;
            lblqq.Text = network.QQ;
            lblmsn.Text = network.Msn;
            lklblblog.Text = network.Blog;
            lklblwebsite.Text = network.WebSite;
        }
        //编辑或保存按钮
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text == "编辑")
            {
                RemoveLblAddTxt();
                btnChangeface.Visible = true;
                btnUpdate.Text = "保存";
                btncancel.Text = "取消";
            }
            else if (btnUpdate.Text == "保存")
            {
                if (ValidateInfo())
                {
                    ContactsInfo contact = PackageContactsInfo();
                    TelInfo tel = PackageTelInfo();
                    AddressInfo addr = PackageAddressInfo();
                    NetworkInfo network = PachageNetworkInfo();
                    if (cBLL.UpdateContact(contact, tel, addr, network) > 0)
                    {
                        RemoveTxtAddLbl();
                        btnUpdate.Text = "编辑";
                        btncancel.Text = "关闭";
                        btnChangeface.Visible = false;
                        frmContact_Load(this, null);
                        AddGroup();
                    }
                }
            }
        }
        //封装联系人
        private ContactsInfo PackageContactsInfo()
        {
            ContactsInfo contact = new ContactsInfo();
            contact.Cid = cid;
            contact.Gid = (int)cbxGroup.SelectedValue;
            contact.Cbirthday = dpbirthday.Value;
            contact.Cname = txtName.Text;
            contact.Csex = cmbSex.Text == "女" ? false : true;
            contact.IdCard = txtIDCard.Text;
            contact.Cface = faceid;
            contact.CBz = txtBz.Text.Trim();
            return contact;
        }
        //封装电话信息
        private TelInfo PackageTelInfo()
        {
            TelInfo tel = new TelInfo();
            tel.Cid = cid;
            tel.Phone = txtphone.Text;
            tel.Phone1 = txtphone1.Text;
            tel.Family = txtfamily.Text;
            tel.Company = txtcompany.Text;
            tel.Fax = txtfax.Text;
            return tel;
        }
        //封装地址信息
        private AddressInfo PackageAddressInfo()
        {
            AddressInfo add = new AddressInfo();
            add.Cid = cid;
            add.Company = txtaddcompany.Text;
            add.Family = txtaddfamily.Text;
            add.Other = txtaddother.Text;
            return add;
        }
        //封装网络信息
        private NetworkInfo PachageNetworkInfo()
        {
            NetworkInfo network = new NetworkInfo();
            network.Cid = cid;
            network.Email = txtEmail.Text;
            network.QQ = txtQQ.Text;
            network.Msn = txtMsn.Text;
            network.Blog = txtBlog.Text;
            network.WebSite = txtWebsite.Text;
            return network;
        }
        //验证信息
        private bool ValidateInfo()
        {
            Regex rx = new Regex(@"^1[358]\d{9}$");

            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                MessageBox.Show("联系人姓名是必填字段，请输入联系人姓名！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            else if (!rx.IsMatch(txtphone.Text) && !string.IsNullOrEmpty(txtphone.Text.Trim()))
            {
                MessageBox.Show("手机号码请输入11位数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtphone.Focus();
                return false;
            }
            else if (!rx.IsMatch(txtphone1.Text) && !string.IsNullOrEmpty(txtphone1.Text.Trim()))
            {
                MessageBox.Show("手机号码请输入11位数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtphone1.Focus();
                return false;
            }
            else if (!Regex.IsMatch(txtEmail.Text, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$") && !string.IsNullOrEmpty(txtEmail.Text.Trim()))
            {
                MessageBox.Show("您输入的电子邮件格式有误，请重新输入！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtEmail.Focus();
                return false;
            }
            return true;
        }
        //移除
        private void RemoveLblAddTxt()
        {
            panel1.Controls.Remove(lblIdCard);
            panel1.Controls.Remove(lklblphone);
            panel1.Controls.Remove(lblphone1);
            panel1.Controls.Remove(lblfamily);
            panel1.Controls.Remove(lblcompany);
            panel1.Controls.Remove(lblfax);
            panel1.Controls.Remove(lbladdfamily);
            panel1.Controls.Remove(lbladdcompany);
            panel1.Controls.Remove(lbladdother);
            panel1.Controls.Remove(lblemail);
            panel1.Controls.Remove(lblqq);
            panel1.Controls.Remove(lblmsn);
            panel1.Controls.Remove(lklblblog);
            panel1.Controls.Remove(lklblwebsite);
            panel1.Controls.Remove(lblBz);
            this.Controls.Remove(lblName);
            this.Controls.Remove(lblSex);
            this.Controls.Remove(lblBirthday);
            this.Controls.Remove(lblGroup);
            this.Controls.Add(txtName);
            this.Controls.Add(dpbirthday);
            this.Controls.Add(cmbSex);
            this.Controls.Add(cbxGroup);
            this.Controls.Add(btnChangeface);
            panel1.Controls.Add(txtIDCard);
            panel1.Controls.Add(txtphone);
            panel1.Controls.Add(txtphone1);
            panel1.Controls.Add(txtfamily);
            panel1.Controls.Add(txtcompany);
            panel1.Controls.Add(txtfax);
            panel1.Controls.Add(txtaddcompany);
            panel1.Controls.Add(txtaddfamily);
            panel1.Controls.Add(txtaddother);
            panel1.Controls.Add(txtEmail);
            panel1.Controls.Add(txtQQ);
            panel1.Controls.Add(txtMsn);
            panel1.Controls.Add(txtBlog);
            panel1.Controls.Add(txtWebsite);
            panel1.Controls.Add(txtBz);
            txtName.Text = lblName.Text.Trim();
            txtIDCard.Text = lblIdCard.Text.Trim();
            txtphone.Text = lklblphone.Text.Trim();
            txtphone1.Text = lblphone1.Text.Trim();
            txtfamily.Text = lblfamily.Text.Trim();
            txtcompany.Text = lblcompany.Text.Trim();
            txtfax.Text = lblfax.Text.Trim();
            txtaddfamily.Text = lbladdfamily.Text.Trim();
            txtaddcompany.Text = lbladdcompany.Text.Trim();
            txtaddother.Text = lbladdother.Text.Trim();
            txtEmail.Text = lblemail.Text.Trim();
            txtQQ.Text = lblqq.Text.Trim();
            txtMsn.Text = lblmsn.Text.Trim();
            txtBlog.Text = lklblblog.Text.Trim();
            txtBz.Text = lblBz.Text.Trim();
            txtWebsite.Text = lklblwebsite.Text.Trim();
            cbxGroup.DataSource = gBLL.GetGroup();
            cbxGroup.DisplayMember = "gname";
            cbxGroup.ValueMember = "gid";
            cbxGroup.SelectedIndex = cbxGroup.FindString(lblGroup.Text);
        }

        private void RemoveTxtAddLbl()
        {
            panel1.Controls.Remove(txtIDCard);
            panel1.Controls.Remove(txtphone);
            panel1.Controls.Remove(txtphone1);
            panel1.Controls.Remove(txtfamily);
            panel1.Controls.Remove(txtcompany);
            panel1.Controls.Remove(txtfax);
            panel1.Controls.Remove(txtaddcompany);
            panel1.Controls.Remove(txtaddfamily);
            panel1.Controls.Remove(txtaddother);
            panel1.Controls.Remove(txtEmail);
            panel1.Controls.Remove(txtQQ);
            panel1.Controls.Remove(txtMsn);
            panel1.Controls.Remove(txtBlog);
            panel1.Controls.Remove(txtWebsite);
            panel1.Controls.Remove(txtBz);
            this.Controls.Remove(txtName);
            this.Controls.Remove(dpbirthday);
            this.Controls.Remove(cmbSex);
            this.Controls.Remove(cbxGroup);
            this.Controls.Remove(btnChangeface);
            panel1.Controls.Add(lblIdCard);
            panel1.Controls.Add(lklblphone);
            panel1.Controls.Add(lblphone1);
            panel1.Controls.Add(lblfamily);
            panel1.Controls.Add(lblcompany);
            panel1.Controls.Add(lblfax);
            panel1.Controls.Add(lbladdfamily);
            panel1.Controls.Add(lbladdcompany);
            panel1.Controls.Add(lbladdother);
            panel1.Controls.Add(lblemail);
            panel1.Controls.Add(lblqq);
            panel1.Controls.Add(lblmsn);
            panel1.Controls.Add(lklblblog);
            panel1.Controls.Add(lklblwebsite);
            panel1.Controls.Add(lblBz);
            this.Controls.Add(lblName);
            this.Controls.Add(lblSex);
            this.Controls.Add(lblBirthday);
            this.Controls.Add(lblGroup);
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            if (btncancel.Text == "关闭")
                this.Close();
            else
            {
                RemoveTxtAddLbl();
                btnUpdate.Text = "编辑";
                btncancel.Text = "关闭";
            }
        }

        //加载分组
        private void AddGroup()
        {
            //读取所有分组信息
            IList<GroupInfo> groups = gBLL.GetGroup();
            int i = 0;
            _sidebar.Groups.Clear();
            foreach (GroupInfo item in groups)
            {
                _sidebar.AddGroup(item.Gname);//填加组名称
                IList<ContactsInfo> contacts = cBLL.GetContactsByGid(item.Gid);//获取该组联系人
                foreach (ContactsInfo contact in contacts)
                {
                    SbItem sbItem = new SbItem();
                    sbItem.Text = contact.Cname;
                    sbItem.Tag = contact.Cid;
                    int monthResult = contact.Cbirthday.Month.CompareTo(DateTime.Now.Month);//比较月份检查
                    int dayResult = contact.Cbirthday.Day.CompareTo(DateTime.Now.Day);//比较当日
                    //如果相册里面的表情数少于保存的数量
                    sbItem.ImageIndex = contact.Cface >= _imglist.Images.Count ? 0 : contact.Cface;
                    _sidebar.Groups[i].Items.Add(sbItem);
                }
                i++;
            }
        }

        private void btnChangeface_Click(object sender, EventArgs e)
        {
            //把当前的头像组传递给头像管理窗口
            FrmFaceManager ffmage = new FrmFaceManager(_imglist, picboxinfo);
            try
            {
                ffmage.ShowDialog(this);
            }
            finally
            {
                ffmage.Dispose();
            }
        }
    }
}
